package br.com.ordersys.web.client.modules.passreset;

import br.com.ordersys.web.client.modules.broker.CallBack;
import br.com.ordersys.web.client.modules.broker.ServiceBroker;
import br.com.ordersys.web.client.modules.broker.ServiceCall;
import br.com.ordersys.web.client.modules.broker.ServiceResponse;
import br.com.ordersys.web.client.modules.uicomp.BasicResources;
import br.com.ordersys.web.client.modules.uicomp.ErrorBox;
import br.com.ordersys.web.client.modules.utils.CryptUtils;
import br.com.ordersys.web.client.modules.utils.I18nBundle;
import br.com.ordersys.web.client.modules.utils.UIUtils;
import br.com.ordersys.web.client.modules.utils.XMLUtils;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;

public class PasswordResetUI implements EntryPoint {
	private ErrorBox errorMsg;
	private I18nBundle i18n = new I18nBundle("passreset");
	
	static{
		BasicResources.INSTANCE.basicStyle().ensureInjected();
	}
	
	@Override
	public void onModuleLoad() {
		buildUI();
	}

	private void buildUI() {
		VerticalPanel vpanel = new VerticalPanel();
		final PasswordTextBox txtPass = new PasswordTextBox();
		final PasswordTextBox txtPass2 = new PasswordTextBox();
		errorMsg = new ErrorBox();
		errorMsg.setStyleName(BasicResources.INSTANCE.basicStyle().error());
		UIUtils.setWidgetStyle(errorMsg, "maxWidth: 256px;");
		
		txtPass.setStyleName(BasicResources.INSTANCE.basicStyle().loginTextField());
		txtPass2.setStyleName(BasicResources.INSTANCE.basicStyle().loginTextField());
		
		UIUtils.setWidgetStyle(txtPass,"marginBottom: 10px");
		UIUtils.setWidgetStyle(txtPass2,"marginBottom: 10px");
		
		Label l = new Label(i18n.get("lbl_new_password"));
		
		l.setStyleName(BasicResources.INSTANCE.basicStyle().loginLabel());
		vpanel.add(l);
		vpanel.add(txtPass);

		l = new Label(i18n.get("lbl_retype_password"));
		l.setStyleName(BasicResources.INSTANCE.basicStyle().loginLabel());
		vpanel.add(l);
		vpanel.add(txtPass2);
		
		Button btnConfirm = new Button(i18n.get("btn_confirm"));
		btnConfirm.setStyleName(BasicResources.INSTANCE.basicStyle().defaultButton());
		
		btnConfirm.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				confirmar(txtPass.getText(),txtPass2.getText());
			}
		});
	
		vpanel.add(errorMsg);
		vpanel.add(btnConfirm);
		vpanel.setCellHorizontalAlignment(btnConfirm, vpanel.ALIGN_RIGHT);
		
		RootPanel mainPnl = RootPanel.get("_main");
		mainPnl.add(vpanel);
	}

	protected void confirmar(String pass, String pass2) {
		errorMsg.clear();
		pass = pass != null ? pass.trim() : "";
		pass2 = pass2 != null ? pass2.trim() : "";
		
		if(pass.length() == 0 ){
			errorMsg.setMessage(i18n.get("msg_error_empty_password"));
			return;
		}
		
		if(!pass.equals(pass2)){
			errorMsg.setMessage(i18n.get("msg_error_retyped_password_dont_match"));
			return;
		}
		
		ServiceCall sc = new ServiceCall("user", "resetPassword");

		Document xml = XMLParser.createDocument();

		Element reqBody = xml.createElement("obj");
		
		reqBody.appendChild(XMLUtils.createElement(xml, "password", CryptUtils.getMD5for(pass)));
		reqBody.appendChild(XMLUtils.createElement(xml, "resetID", UIUtils.getEnvironment("resetId")));

		sc.setServiceBody(reqBody);
		sc.setHandleErrors(true);

		sc.setResponseCallBack(new CallBack() {
			@Override
			public void onResponse(ServiceResponse response) {
				if(response.getStatus() == 0){
					Window.alert(i18n.get("msg_sucessful_password_changed"));
					Window.Location.assign(UIUtils.buildAppBaseURI("Login.page"));
				}else{
					errorMsg.setMessage(response.getMessage());
				}
			}
		});

		ServiceBroker sb = new ServiceBroker();

		sb.call(sc);

	}
}
